//
//  KYCustomPlaningView.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/10/17.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYCustomPlaningView.h"

@interface KYCustomPlaningView()
/**
 *  关闭图片
 */
@property (nonatomic, weak) UIButton *cancelBtn;
/**
 *  背景
 */
@property (nonatomic, weak) UIView *bgView;
/**
 *  展现内容的view
 */
@property (nonatomic, weak) UIView *contentView;
/**
 *  头部的背景
 */
@property (nonatomic, weak) UIImageView *headerBgView;
/**
 *  标题
 */
@property (nonatomic, weak) UILabel *titleLabel;
/**
 *  tips
 */
@property (nonatomic, weak) UIView *tipsView0;
@property (nonatomic, weak) UILabel *tipsLabel0;
@property (nonatomic, weak) UIView *tipsView1;
@property (nonatomic, weak) UILabel *tipsLabel1;
@property (nonatomic, weak) UIView *tipsView2;
@property (nonatomic, weak) UILabel *tipsLabel2;
@property (nonatomic, weak) UIView *tipsView3;
@property (nonatomic, weak) UILabel *tipsLabel3;
/**
 *  确定
 */
@property (nonatomic, weak) UIButton *sureBtn;

@end

@implementation KYCustomPlaningView
+ (instancetype)customPlaningView {
    return [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUpSubViews];
    }
    
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)decoder {
    if (self = [super initWithCoder:decoder]) {
        [self setUpSubViews];
    }
    
    return self;
}

- (void)setUpSubViews
{
    // 0.初始化子控件
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.alpha = 0.65;
    [self addSubview:bgView];
    self.bgView = bgView;
    
    UIButton *cancelBtn = [[UIButton alloc] init];
    [cancelBtn setImage:[UIImage ky_imageNamed:@"checkIn_planingView_cancel"] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchDown];
    [self addSubview:cancelBtn];
    self.cancelBtn = cancelBtn;
    
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.layer.cornerRadius = 4;
    contentView.layer.masksToBounds = YES;
    [self addSubview:contentView];
    self.contentView = contentView;
    
    UIImageView *headerBgView = [[UIImageView alloc] init];
    headerBgView.backgroundColor = KYColor(246, 211, 91, 1.0);
    [contentView addSubview:headerBgView];
    self.headerBgView = headerBgView;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = NSLocalizedString(@"签到攻略", nil);
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:20.5f];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    UIView *tipsView0 = [[UIView alloc] init];
    tipsView0.backgroundColor = [UIColor redColor];
    tipsView0.layer.masksToBounds = YES;
    [contentView addSubview:tipsView0];
    self.tipsView0 = tipsView0;
    
    UILabel *tipsLabel0 = [[UILabel alloc] init];
    tipsLabel0.text = NSLocalizedString(@"每日签到随机获取 1、2、3 个快币.", nil);
    tipsLabel0.textColor = [UIColor grayColor];
    tipsLabel0.font = [UIFont systemFontOfSize:16.0f];
    tipsLabel0.textAlignment = NSTextAlignmentLeft;
    tipsLabel0.numberOfLines = 0;
    [contentView addSubview:tipsLabel0];
    self.tipsLabel0 = tipsLabel0;
    
    UIView *tipsView1 = [[UIView alloc] init];
    tipsView1.backgroundColor = [UIColor orangeColor];
    tipsView1.layer.masksToBounds = YES;
    [contentView addSubview:tipsView1];
    self.tipsView1 = tipsView1;
    
    UILabel *tipsLabel1 = [[UILabel alloc] init];
    tipsLabel1.text = NSLocalizedString(@"连续签到的第7天／14天／21天／28天,可获得额外奖励大礼包.", nil);
    tipsLabel1.textColor = [UIColor grayColor];
    tipsLabel1.font = [UIFont systemFontOfSize:16.0f];
    tipsLabel1.textAlignment = NSTextAlignmentLeft;
    tipsLabel1.numberOfLines = 0;
    [contentView addSubview:tipsLabel1];
    self.tipsLabel1 = tipsLabel1;
    
    UIView *tipsView2 = [[UIView alloc] init];
    tipsView2.backgroundColor = KYColor(171, 222, 249, 1.0);
    tipsView2.layer.masksToBounds = YES;
    [contentView addSubview:tipsView2];
    self.tipsView2 = tipsView2;
    
    UILabel *tipsLabel2 = [[UILabel alloc] init];
    tipsLabel2.text = NSLocalizedString(@"28天为一个连续签到周期,签满28天从第一天开始计数,中断后也需要从第一天开始计数.", nil);
    tipsLabel2.textColor = [UIColor grayColor];
    tipsLabel2.font = [UIFont systemFontOfSize:16.0f];
    tipsLabel2.textAlignment = NSTextAlignmentLeft;
    tipsLabel2.numberOfLines = 0;
    [contentView addSubview:tipsLabel2];
    self.tipsLabel2 = tipsLabel2;
    
    UIView *tipsView3 = [[UIView alloc] init];
    tipsView3.backgroundColor = [UIColor purpleColor];
    tipsView3.layer.masksToBounds = YES;
    [contentView addSubview:tipsView3];
    self.tipsView3 = tipsView3;
    
    UILabel *tipsLabel3 = [[UILabel alloc] init];
    tipsLabel3.text = NSLocalizedString(@"同设备／同账号每天仅可签到一次.", nil);
    tipsLabel3.textColor = [UIColor grayColor];
    tipsLabel3.font = [UIFont systemFontOfSize:16.0f];
    tipsLabel3.textAlignment = NSTextAlignmentLeft;
    tipsLabel3.numberOfLines = 0;
    [contentView addSubview:tipsLabel3];
    self.tipsLabel3 = tipsLabel3;
    
    UIButton *sureBtn = [[UIButton alloc] init];
    sureBtn.backgroundColor = KYColor(238, 124, 47, 1.0);
    sureBtn.layer.cornerRadius = 20;
    sureBtn.layer.masksToBounds = YES;
    [sureBtn setTitle:NSLocalizedString(@"好,我知道了", nil) forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchDown];
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:18.5f];
    [contentView addSubview:sureBtn];
    self.sureBtn = sureBtn;
}

#pragma mark - method
- (void)buttonClick
{
    [self hide];
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.bgView.frame = self.bounds;
    
    CGFloat contentViewX = self.frame.size.width * 0.5;
    CGFloat contentViewY = self.frame.size.height * 0.5;
    CGFloat contentViewW = self.frame.size.width * 0.8;
    CGFloat contentViewH = 333.5;
    self.contentView.center = CGPointMake(contentViewX, contentViewY);
    self.contentView.bounds = CGRectMake(0, 0, contentViewW, contentViewH);

    CGFloat cancelBtnW = 30;
    CGFloat cancelBtnX = CGRectGetMaxX(self.contentView.frame) - 0.5 * cancelBtnW - 5;
    CGFloat cancelBtnY = contentViewY - 0.5 * contentViewH - cancelBtnW - 10;
    CGFloat cancelBtnH = cancelBtnW;
    self.cancelBtn.frame = CGRectMake(cancelBtnX, cancelBtnY, cancelBtnW, cancelBtnH);

    CGFloat titleLabelX = 14;
    CGFloat titleLabelY = 0;
    CGFloat titleLabelW = contentViewW - 2 * titleLabelX;
    CGFloat titleLabelH = 55;
    self.titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
    
    CGFloat headerBgViewX = 0;
    CGFloat headerBgViewY = 0;
    CGFloat headerBgViewW = contentViewW;
    CGFloat headerBgViewH = titleLabelH;
    self.headerBgView.frame = CGRectMake(headerBgViewX, headerBgViewY, headerBgViewW, headerBgViewH);
    
    CGFloat tipsView0X = titleLabelX;
    CGFloat tipsView0Y = CGRectGetMaxY(self.titleLabel.frame) + 23;
    CGFloat tipsView0W = 7;
    CGFloat tipsView0H = tipsView0W;
    self.tipsView0.layer.cornerRadius = tipsView0W / 2;
    self.tipsView0.frame = CGRectMake(tipsView0X, tipsView0Y, tipsView0W, tipsView0H);
    
    CGFloat tipsLabel0X = CGRectGetMaxX(self.tipsView0.frame) + 10;
    CGFloat tipsLabel0Y = CGRectGetMaxY(self.titleLabel.frame) + 15;
    CGFloat tipsLabel0W = titleLabelW - tipsView0W - 10;
    CGFloat tipsLabel0H = 25;
    self.tipsLabel0.frame = CGRectMake(tipsLabel0X, tipsLabel0Y, tipsLabel0W, tipsLabel0H);
    
    CGFloat tipsView1X = titleLabelX;
    CGFloat tipsView1Y = CGRectGetMaxY(self.tipsLabel0.frame) + 17;
    CGFloat tipsView1W = tipsView0W;
    CGFloat tipsView1H = tipsView0W;
    self.tipsView1.layer.cornerRadius = tipsView1W / 2;
    self.tipsView1.frame = CGRectMake(tipsView1X, tipsView1Y, tipsView1W, tipsView1H);
    
    CGFloat tipsLabel1X = tipsLabel0X;
    CGFloat tipsLabel1Y = CGRectGetMaxY(self.tipsLabel0.frame) + 10;
    CGFloat tipsLabel1W = tipsLabel0W;
    CGFloat tipsLabel1H = 40;
    self.tipsLabel1.frame = CGRectMake(tipsLabel1X, tipsLabel1Y, tipsLabel1W, tipsLabel1H);
    
    CGFloat tipsView2X = titleLabelX;
    CGFloat tipsView2Y = CGRectGetMaxY(self.tipsLabel1.frame) + 17;
    CGFloat tipsView2W = tipsView0W;
    CGFloat tipsView2H = tipsView0W;
    self.tipsView2.layer.cornerRadius = tipsView2W / 2;
    self.tipsView2.frame = CGRectMake(tipsView2X, tipsView2Y, tipsView2W, tipsView2H);
    
    CGFloat tipsLabel2X = tipsLabel0X;
    CGFloat tipsLabel2Y = CGRectGetMaxY(self.tipsLabel1.frame) + 10;
    CGFloat tipsLabel2W = tipsLabel0W;
    CGFloat tipsLabel2H = 58;
    self.tipsLabel2.frame = CGRectMake(tipsLabel2X, tipsLabel2Y, tipsLabel2W, tipsLabel2H);
    
    CGFloat tipsView3X = titleLabelX;
    CGFloat tipsView3Y = CGRectGetMaxY(self.tipsLabel2.frame) + 17;
    CGFloat tipsView3W = tipsView0W;
    CGFloat tipsView3H = tipsView0W;
    self.tipsView3.layer.cornerRadius = tipsView2W / 2;
    self.tipsView3.frame = CGRectMake(tipsView3X, tipsView3Y, tipsView3W, tipsView3H);
    
    CGFloat tipsLabel3X = tipsLabel0X;
    CGFloat tipsLabel3Y = CGRectGetMaxY(self.tipsLabel2.frame) + 10;
    CGFloat tipsLabel3W = tipsLabel0W;
    CGFloat tipsLabel3H = tipsLabel0H;
    self.tipsLabel3.frame = CGRectMake(tipsLabel3X, tipsLabel3Y, tipsLabel3W, tipsLabel3H);
    
    CGFloat sureBtnH = 40;
    CGFloat sureBtnX = contentViewW * 0.5;
    CGFloat sureBtnY = CGRectGetMaxY(self.tipsLabel3.frame) + sureBtnH * 0.5 + 25;
    CGFloat sureBtnW = 180;
    self.sureBtn.center = CGPointMake(sureBtnX, sureBtnY);
    self.sureBtn.bounds = CGRectMake(0, 0, sureBtnW, sureBtnH);
}

#pragma mark - public
/**
 *  显示可连接设备View
 */
- (void)show {
    [[[UIApplication sharedApplication].delegate window] addSubview:self];
}

/**
 *  隐藏可连接设备View
 */
- (void)hide {
    [self removeFromSuperview];
}
@end
